package net.phoenix.repository;

import java.sql.Connection;
import java.util.Map;

import javax.jcr.LoginException;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.commons.collections.map.LRUMap;

/**
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-2
 * @version 1.0.0
 */
public class BasicSession extends JdbcSession {
	private Map<String, Node> cache;
	private final static int CACHE_SIZE = 1024;

	public BasicSession(BasicRepository repository, Connection connection,
			String workspaceName) throws LoginException,
			NoSuchWorkspaceException, RepositoryException {
		super(repository, connection, workspaceName);
		this.cache = new LRUMap(CACHE_SIZE);
	}

	@SuppressWarnings("unchecked")
	public void setCacheSize(int size) {
		this.cache = new LRUMap(size);
	}

	@Override
	public Node getNodeByIdentifier(String nodeId) throws RepositoryException {
		if (nodeId == null) {
			throw new IllegalArgumentException("nodeId should not be null.");
		}
		Node node = (Node) this.cache.get(nodeId);
		if (node != null) {
			return node;
		}

		node = super.getNodeByIdentifier(nodeId);
		this.cache.put(node.getIdentifier(), node);
		return node;
	}

}
